import {add, deleteById, getById, getListForPage, update} from '../../services/logistics/logisticsPort';
import {message} from 'antd';
import Constants from '../../utils/Constants'
const R = require('ramda');

export default {
    namespace: 'port',
    state: {
        page: {},
        list: [],
        dataSource: {}
    },
    reducers: {
        /**
         * 刷新state
         *
         * @param state
         */
        refreshState(state, {payload}) {
            return {...state, ...payload};
        }
    },
    effects: {
        /**
         * 分页获取物流订单列表
         *
         * @param call
         * @param put
         *
         */
        *getListForPage({payload}, {call, put}) {
            const res = yield call(getListForPage, {...R.omit(['onSuccess', 'onFail'])(payload)});

            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: res.data.content.list,
                        page: res.data.content.page
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 根据id获取物流订单
         *
         * @param id
         */
        *getById({payload}, {call, put}) {
            if (payload.id) {
                const res = yield call(getById, {...R.omit(['onSuccess', 'onFail'])(payload)});
                if (res && res.data.resultCode === "ok") {
                    yield put({
                        type: 'refreshState',
                        payload: {
                            dataSource: res.data.content
                        }
                    });
                } else {
                    if (payload.onFail) payload.onFail();
                }
            } else {
                yield put({
                    type: 'refreshState',
                    payload: {
                        dataSource: {}
                    }
                });
            }
        },
        /**
         * 新增物流订单
         *
         * @param call
         */
        *addOrder({payload}, {call}) {
            const res = yield call(add, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                message.info("物流订单新增成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 修改港口
         *
         * @param call
         * @param put
         * @param select
         */
        *updateOrder({payload}, {call, put}) {
            const res = yield call(update, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                message.info("物流订单更新成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 删除物流订单
         *
         * @param id
         */
        *deleteById({payload}, {call}) {
            const res = yield call(deleteById, {...R.omit(['onSuccess', 'onFail'])(payload)});
            if (res && res.data.resultCode === "ok") {
                message.info("物流订单删除成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        }
    },
    subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {
                const menus = JSON.parse(sessionStorage.getItem(Constants.USER_MENU));
                if(menus && !menus.find(item=>item.includes(pathname)) && pathname != '/logout'){
                    document.location.href = "/notAuthorized"
                }
            });
        }
    }
};